Release 10.1A: OpenEdge Getting Started:
Database Essentials


Indexing basics

This section explains the basics of indexing. Topics include:

How indexes work

A database index works like a book index. To look up a topic, you scan the book index, locate the topic, and turn to the pages where the information resides. The index itself does not contain the information; it only contains page numbers that direct you to the pages where the information resides. Without an index, you would have to search the entire book, scanning each page sequentially.

Similarly, if you ask for specific data from a database, the database engine uses an index to find the data. An index contains two pieces of information—the index key and a row pointer that points to the corresponding row in the main table. Figure 2–6 illustrates this using the Order table from Sports 2000 database.

Figure 2–6: Indexing the Order table

The index table entries are always sorted in numerical, alphabetical, or chronological order. Using the pointers, the system can then access data rows directly and in the sort order specified by the index.

Every table should have at least one index, the primary index. When you create the first index on any table, OpenEdge assumes it is the primary index and sets the Primary flag accordingly. In Figure 2–6, the Order-Num index is the primary index.

Reasons for defining an index

There are four significant benefits to defining an index for a table:

Sample indexes

Table 2–9 lists some indexes defined in the Sports 2000 database, showing why the index was defined.

Table 2–9: Reasons for defining some Sports 2000 database indexes
Table
Index name
Index column(s)
Primary
Unique
Customer
cust-num
cust-num
YES
YES
Why the index was defined:
  • Rapid access to a customer given a customer’s number.
  • Reporting customers in order by number.
  • Ensuring that there is only one customer row for each customer number (uniqueness).
  • Rapid access to a customer from an order, using the customer number in the order row.
name
name
NO
NO
Why the index was defined:
  • Rapid access to a customer given a customer’s name.
  • Reporting customers in order by name.
zip
zip
NO
NO
Why the index was defined:
  • Rapid access to all customers with a given zip code or in a zip code range.
  • Reporting customers in order by zip code, perhaps for generating mailing lists.
Item
item-num
item-num
YES
YES
Why the index was defined:
  • Rapid access to an item given an item number.
  • Reporting items in order by number.
  • Ensuring that there is only one item row for each item number (uniqueness).
  • Rapid access to an item from an order-line, using the item-num column in the order-line row.
Order-line
order-line
order-num
line-num
YES
YES
Why the index was defined:
  • Ensuring that there is only one order-line row with a given order number and line number. The index is based on both columns together since neither column alone needs to be unique.
  • Rapid access to all of the order-lines for an order, ordered by line number.
item-num
item-num
NO
NO
Why the index was defined:
  • Rapid access to all the order-lines for a given item.
Order
order-num
order-num
YES
YES
Why the index was defined:
  • Rapid access to an order given an order number.
  • Reporting orders in order by number.
  • Ensuring that there is only one order row for each order number (uniqueness).
  • Rapid access to an order from an order-line, using the order-num column in the order-line row.
cust-order
cust-num
order-num
NO
YES
Why the index was defined:
  • Rapid access to all the orders placed by a customer. Without this index, all of the records in the order file would be examined to find those having a particular value in the cust-num column.
  • Ensuring that there is only one row for each customer/order combination (uniqueness).
  • Rapid access to the order numbers of a customer’s orders.
order-date
order-date
NO
NO
Why the index was defined:
  • Rapid access to all the orders placed on a given date or in a range of dates.

Disadvantages of defining an index

Even though indexes are beneficial, there are two things to keep in mind when defining indexes for your database:

Define the indexes that your application requires, but avoid indexes that provide minor benefit or are infrequently used. For example, unless you display data in a particular order frequently (such as by zip code), then you are better off sorting the data when you display it instead of defining an index to do automatic sorting.


Copyright © 2005 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095